#include "wmi_filedirectory.h"

using namespace std;
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;

WMI_FileDirectory::WMI_FileDirectory()
{}

WMI_FileDirectory::WMI_FileDirectory(IWbemClassObject* pclsObj)
{
	_pclsObj = pclsObj;
	setProperties();
    DOMConfigurator::configure("LogConfig.xml");
}

WMI_FileDirectory::~WMI_FileDirectory()
{

}

void 
WMI_FileDirectory::setProperties()
{
	VARIANT vtProp;
	HRESULT hr;

	hr = _pclsObj->Get(bstr_t(NAME.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {            
        setName(wstring(vtProp.bstrVal));
    }

    hr = _pclsObj->Get(bstr_t(CAPTION.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setCaption(wstring(vtProp.bstrVal));
    }

    hr = _pclsObj->Get(bstr_t(STATUS.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setStatus(wstring(vtProp.bstrVal));
    }

    hr = _pclsObj->Get(bstr_t(DESCRIPTION.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setDescription(wstring(vtProp.bstrVal));
    }

    hr = _pclsObj->Get(bstr_t(INSTALL_DATE.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr) && vtProp.vt != 1)
    {
        setInstallDate(WMI_Utils::wcharToCTime(vtProp.bstrVal));            
    }

    hr = _pclsObj->Get(bstr_t(CREATION_DATE.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setCreationDate(WMI_Utils::wcharToCTime(vtProp.bstrVal));            
    }

    hr = _pclsObj->Get(bstr_t(LAST_ACCESSED.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setLastAccessed(WMI_Utils::wcharToCTime(vtProp.bstrVal));            
    }

    hr = _pclsObj->Get(bstr_t(LAST_MODIFIED.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setLastModified(WMI_Utils::wcharToCTime(vtProp.bstrVal));            
    }

    hr = _pclsObj->Get(bstr_t(WRITEABLE.c_str()), 0, &vtProp, 0, 0);
    if(SUCCEEDED(hr))
    {
        setWriteable(vtProp.boolVal);            
    }

	VariantClear(&vtProp);

}

void WMI_FileDirectory::print()
{
    wstringstream wout;
    LoggerPtr loggerFileDir(Logger::getLogger("loggerFileDir"));

    string date=WMI_Utils::formatStd(getCreationDate());
    wstring wdate(date.begin(),date.end());
    wout << NAME <<": \""<< getName(); 
    wout <<  "\" " << CREATION_DATE <<": ";
    wout << wdate;
    
    LOG4CXX_INFO(loggerFileDir,wout.str());

}